//
//  SIScrollView.m
//  UI06_UIScrollView
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 司昌宝. All rights reserved.
//

#import "SIScrollView.h"

@implementation SIScrollView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        [self setProtertys];
    }
    return self;
}

-(void)setProtertys{
    /** 
     * 重点
     * frame设置的是scrollView的可视范围,contentSize设置的是scrollView的滚动范围.
     * 当contentSize的width大于frame的width时,水平方向可以滚动,小于等于时水平方向不可以滚动.
     * 当contentSize的height大于frame的height时,垂直方向可以滚动,小于等于时垂直方向不可以滚动.
     */
    self.contentSize = CGSizeMake(self.frame.size.width * 3, 0);
    /** 不显示滑动条 */
    /** 水平 */
    self.showsHorizontalScrollIndicator = NO;
    /** 垂直 */
    self.showsVerticalScrollIndicator = NO;
    
    /** 设置分页效果. */
    self.pagingEnabled = YES;
    
    /** 回弹效果,默认开启. */
    self.bounces = YES;
    /** 最大倍率和最小倍率,图片的 */
//    self.maximumZoomScale = 3.0;
//    self.minimumZoomScale = 0.5;
}

/** 添加图片. */
-(void)addPictures:(NSArray<UIImage *> *)pictureArray{
    NSMutableArray *temp = [NSMutableArray arrayWithArray:pictureArray];
    [temp insertObject:pictureArray.lastObject atIndex:0];
    [temp addObject:pictureArray[0]];
    
    
    
    /** 确定滚动范围 */
    self.contentSize = CGSizeMake(CGRectGetWidth(self.bounds) * temp.count, 0);
    
    for(int i = 0; i < temp.count;i++){
        /** 创建UIImageView */
        UIImageView *imageView = [[UIImageView alloc] initWithImage:temp[i]];
        imageView.frame= CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
        
        UIScrollView *scrollViewOfImage = [[UIScrollView alloc] initWithFrame:CGRectMake(i * CGRectGetWidth(self.bounds), 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))];
       
        [scrollViewOfImage addSubview:imageView];
        [self addSubview:scrollViewOfImage];
        
        
        [imageView release];
        [scrollViewOfImage release];
    }
    
}

@end
